<script setup lang="ts">
import { ref, reactive } from "vue"
import { PasswordRegex } from "#/utils/constants"
import { updateUserAdminApi } from '#/api/system/user'
import setPublicCode from "#/utils/setPublicKey"
import { $t } from '#/locales'

defineOptions({ name: 'Dialog' })

const props = defineProps({
  userData: {
    type: Object,
    default: () => { }
  }
});

const passwordErrMsg = $t('user.passwordErrMsgTxt')
const emit = defineEmits(['queryTable'])
const submitLoading = ref(false)
const dialogVisible = ref(false)
const formRef = ref()
const defaultFormInline = {
  loginPassword: '',
  checkLoginPassword: ''
}
const formInline = reactive({ ...defaultFormInline })

const rules = {

  loginPassword: [
    { required: true, pattern: PasswordRegex, message: passwordErrMsg, trigger: 'blur' },
  ],
  checkLoginPassword: [
    {
      required: true, validator: (rule: any, value: any, callback: any) => {
        console.log(rule)
        if (!value) {
          callback(new Error($t('common.confirmTxt', [$t('user.newPassTxt')])))
        } else {
          if (!passwordCheck(value)) {
            callback(new Error(passwordErrMsg))
          } else if (formInline.checkLoginPassword && value !== formInline.loginPassword) {
            callback(new Error($t('user.passNotSameTxt')))
          } else {
            formRef.value?.clearValidate(['checkLoginPassword'])
          }
        }
        callback()

      }, trigger: 'blur'
    }
  ]
}
const passwordCheck = (val: string) => {
  return PasswordRegex.test(val)
}
const handleClose = () => {
  Object.assign(formInline, defaultFormInline)
  formRef.value.resetFields()
  dialogVisible.value = false
}

const inint = () => {
  console.log(1)
  dialogVisible.value = true

}
const submit = () => {
  if (!formRef) return
  formRef.value?.validate((valid: any) => {
    if (valid) {
      let params = {
        loginPassword: setPublicCode(formInline.loginPassword),
        id: props.userData.userId
      }
      submitLoading.value = true
      updateUserAdminApi(params).then(() => {
        emit('queryTable')
        handleClose()
      }).finally(() => {
        submitLoading.value = false
      })
    }
  })
}

defineExpose({ inint })

</script>
<template>
  <el-dialog v-model="dialogVisible" :destroy-on-close="true" :title="$t('user.changePassTxt')" width="600"
    :append-to-body="true" :close-on-click-modal="false" :before-close="handleClose">
    <el-form ref="formRef" :model="formInline" :rules="rules" label-width="100px">
      <el-form-item :label="$t('user.newPassTxt')" prop="loginPassword">
        <el-input type="password" :show-password="true" v-model="formInline.loginPassword" clearable
          :placeholder="$t('common.inputPhTxt', [$t('user.newPassTxt')])" maxlength="16" />
      </el-form-item>
      <el-form-item :label="$t('common.submitTxt', [$t('user.newPassTxt')])" prop="checkLoginPassword">
        <el-input type="password" :show-password="true" v-model="formInline.checkLoginPassword" clearable
          :placeholder="$t('common.confirmTxt', [$t('user.newPassTxt')])" maxlength="16" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">
          {{ $t('common.cancelTxt') }}
        </el-button>
        <el-button type="primary" @click="submit" :loading="submitLoading">
          {{ $t('common.submitTxt') }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<style lang="less" scoped></style>
